package powerControl.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import powerControl.dao.IUserDao;
import powerControl.entity.Resource;
import powerControl.entity.User;

public class UserDaoImpl extends HibernateDaoSupport implements IUserDao {
	/**
	 * 根据用户和url判断是否具有访问权限
	 * @param user
	 * @param url
	 * @return
	 */
	public boolean checkPowerForUserAndUrl(final User user, final String url) {
		// 定义tempList
		List tempList = null;

		HibernateCallback cell = new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// 用session处理数据
				Query query = null;
				List list = null;
				List list2 = null;
				String uuidOfResource = null;
				// 根据url可以查找到RESOURCE_对象。
				System.out.println("\n\n\n-----------------------  开始查找RESOURCE_对象\n\n\n");
				query = session
						.createQuery("select r.uuid from Resource r where r.url = :url");
				query.setString("url", url);
				list = query.list();
				if (list.size() == 0) {
					System.out.println("\n\n\n-----------------------  未查到RESOURCE_对象\n\n\n");
					return false;
				} else {
					uuidOfResource = (String) list.get(0);
				}
				// 根据RESOURCE_对象可以查找到role集合。
				System.out.println("\n\n\n-----------------------  开始查找第一个role集合\n\n\n");
				query = session
						.createQuery("select r.role from RoleResourceRelation r inner join r.resource where r.resource.uuid = :uuidOfResource");
				query.setString("uuidOfResource", uuidOfResource);
				list = query.list();
				if (list.size() == 0) {
					System.out.println("\n\n\n-----------------------  未查到第一个role集合\n\n\n");
					return false;
				}
				// 根据user可以查找到另一个role集合。
				System.out.println("\n\n\n-----------------------  开始查找第二个role集合\n\n\n");
				query = session
						.createQuery("select u.role from UserRoleRelation u inner join u.user where u.user.uuid = :uuidOfuser");
				System.out.println("\n\n\n-----------------------  标记一\n\n\n");
				query.setString("uuidOfuser", user.getUuid() );
				System.out.println("\n\n\n-----------------------  标记二\n\n\n");
				list2 = query.list();
				if (list2.size() == 0) {
					System.out.println("\n\n\n-----------------------  未查到第二个role集合\n\n\n");
					return false;
				}
				// 遍历两个role集合，看是否有重合的role对象。
				System.out.println("\n\n\n-----------------------  开始查找重合的role对象\n\n\n");
				for (int i = 0; i < list.size(); i++) {
					for (int j = 0; j < list2.size(); j++) {
						if (list.get(i) == list2.get(j)) {
							System.out.println("\n\n\n-----------------------  已通过权限审查\n\n\n");
							return true;
						}
					}
				}
				System.out.println("\n\n\n-----------------------  未查到重合的role对象\n\n\n");
				return false;
			}
		};
		return super.getHibernateTemplate().execute(cell);
	}

	public User findByNameAndPassword(User user) {
		User resultUser = null;
		List list = super.getHibernateTemplate().findByExample(user);
		if (list.size() > 0) {
			resultUser = (User) list.get(0);
		}
		return resultUser;
	}

	/**
	 * 此方法演示获取session对象,然后用session对象处理持久化操作
	 * @param a
	 */
	public void add(final String a) {
		HibernateCallback cell = new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				System.out.println(a);
				return null;
			}
		};
		this.getHibernateTemplate().execute(cell);
	}
}